package com.baidu.erp.web;

import com.alibaba.fastjson.JSONObject;
import com.baidu.erp.domain.Permission;
import com.baidu.erp.domain.Role;
import com.baidu.erp.sercvice.PermissionService;
import com.baidu.erp.sercvice.RoleService;
import com.baidu.erp.sercvice.impl.PermissionServiceImpl;
import com.baidu.erp.sercvice.impl.RoleServiceImpl;
import org.apache.commons.beanutils.BeanUtils;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.lang.reflect.InvocationTargetException;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Optional;

@WebServlet("/role")
public class RoleServlet extends BaseServlet {
    /**
     * 跳转到主页
     */
    public void index(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        request.getRequestDispatcher("WEB-INF/qx/role/index.jsp").forward(request, response);
    }

    /**
     * 跳转到 list.jsp
     */
    public void list(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        request.getRequestDispatcher("WEB-INF/qx/role/list.jsp").forward(request, response);
    }

    /**
     * 跳转到 add.jsp
     */
    public void add(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        request.getRequestDispatcher("WEB-INF/qx/role/add.jsp").forward(request, response);
    }

    /**
     * 跳转到 edit.jsp 修改
     */
    public void edit(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        request.getRequestDispatcher("WEB-INF/qx/role/edit.jsp").forward(request, response);
    }

    /**
     * 跳转到 assign.jsp 分配许可
     */
    public void assign(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        request.getRequestDispatcher("WEB-INF/qx/role/assign.jsp").forward(request, response);
    }

    /**
     * 跳转到 详情 页面
     */
    public void detail(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        String id = request.getParameter("id");
        RoleService roleService = new RoleServiceImpl();
        Role role = roleService.getById(id);
        request.setAttribute("role", role);
        request.getRequestDispatcher("WEB-INF/qx/role/detail.jsp").forward(request, response);
    }

    /**
     * 新增角色
     */
    public void save(HttpServletRequest request, HttpServletResponse response) throws InvocationTargetException, IllegalAccessException, IOException {
        response.setContentType("text/json;charset=UTF-8");//"text/json;charset=UTF-8"
        Map<String, String[]> map = request.getParameterMap();
        Role role = new Role();
        BeanUtils.populate(role, map);
        RoleService roleService = new RoleServiceImpl();
        int count = roleService.save(role);
        Map<String, String> jsonMap = new HashMap();
        if (count > 0) {
            jsonMap.put("code", "200");
            jsonMap.put("mesg", "保存成功");
        } else {
            jsonMap.put("code", "500");
            jsonMap.put("mesg", "保存失败");
        }
        response.getWriter().println(JSONObject.toJSONString(jsonMap));
    }

    /**
     * 根据名字查询角色
     */
    public void getRoleByName(HttpServletRequest request, HttpServletResponse response) throws IOException {
        response.setContentType("text/json;charset=UTF-8");
        String name = request.getParameter("name");
        RoleService roleService = new RoleServiceImpl();
        Optional<Role> optionalRole = roleService.getRoleByName(name);
        Map<String, String> jsonMap = new HashMap();
        jsonMap.put("code", "200");
        optionalRole.ifPresent(p -> jsonMap.put("code", "500"));
        response.getWriter().println(JSONObject.toJSONString(jsonMap));
    }

    /**
     * 根据页码显示数据
     */
    public void getByPage(HttpServletRequest request, HttpServletResponse response) throws IOException {
        response.setContentType("text/json;charset=UTF-8");
        String pageNo = request.getParameter("pageNo");
        String pageSize = request.getParameter("pageSize");

        RoleService roleService = new RoleServiceImpl();
        List<Role> roleList = roleService.getByPage(Integer.parseInt(pageNo), Integer.parseInt(pageSize));
        response.getWriter().println(JSONObject.toJSONString(roleList));

    }

    /**
     * 根据角色id 查询权限列表
     */
    public void getPermissionList(HttpServletRequest request, HttpServletResponse response) throws IOException {
        response.setContentType("text/json;charset=UTF-8");
        String roleId = request.getParameter("roleId");
        PermissionService permissionService = new PermissionServiceImpl();
        List<Permission> permissionList = permissionService.getPermissionByRoleId(roleId);
        response.getWriter().println(JSONObject.toJSONString(permissionList));
    }

    /**
     * 删除角色
     */
    public void del(HttpServletRequest request,HttpServletResponse response) throws IOException {
        response.setContentType("text/json;charset=UTF-8");
        String ids = request.getParameter("ids");
        String[] idArr = ids.split("-");
        RoleService roleService = new RoleServiceImpl();
        int count = roleService.del(idArr);
        Map<String, String> jsonMap = new HashMap();
        if (count > 0) {
            jsonMap.put("code", "200");
            jsonMap.put("mesg", "删除成功");
        } else {
            jsonMap.put("code", "500");
            jsonMap.put("mesg", "删除失败");
        }
        response.getWriter().println(JSONObject.toJSONString(jsonMap));

    }
}
